/**
 * 
 */
package com.antilia.infomaresme.data.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.antilia.infomaresme.data.dao.interfaces.ICategoryAlertsDao;
import com.antilia.infomaresme.data.dataobjects.CategoryAlert;
import com.antilia.infomaresme.data.dataobjects.User;

/**
 * @author Ernesto
 *
 */
public class CategoryAlertsDao extends AbstractDaoHibernateImpl<CategoryAlert> implements ICategoryAlertsDao {

	/**
	 * @param domainClass
	 */
	public CategoryAlertsDao() {
		super(CategoryAlert.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<CategoryAlert> getItems(User user, int start,int size) {
		Criteria criteria = getSession().get().createCriteria(CategoryAlert.class);
		criteria.setFetchSize(size);
		criteria.setFirstResult(start);
		configureCriteria(criteria, user);
		return (List<CategoryAlert>)criteria.list();
	}
	
	private void configureCriteria(Criteria criteria, User user) {
		if(user != null) {
			criteria.add(Restrictions.eq("user", user));
		}
	}

	public Long countItems(User user) {
		Criteria criteria = session.get().createCriteria(CategoryAlert.class);
		criteria.setProjection(Projections.rowCount());
		configureCriteria(criteria, user);
		return (Long)(criteria.uniqueResult());
	}

	
	public List<CategoryAlert> findAlerts(User user) {
		Criteria criteria = getSession().get().createCriteria(CategoryAlert.class);
		configureCriteria(criteria, user);
		@SuppressWarnings("unchecked")
		List<CategoryAlert> list = (List<CategoryAlert>)criteria.list();
		return list;
	}

}
